package com.xkang.core;

import com.xkang.model.MetaSchema;
import com.xkang.util.FakeUtils;
import com.xkang.util.RegUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @author xiakang
 * @date 2020/6/14 17:52
 * @description
 */
public class BaseDb {



    public String fakeColumns(String filePath){
        StringBuilder sb = new StringBuilder();
        List<MetaSchema> metaSchemas = FakeUtils.parseMetaSchema(filePath);
        for (MetaSchema metaSchema : metaSchemas) {
            String keyword = RegUtils.reg_all_keywords(metaSchema.getComment());

            Object o = this.doFake(keyword);
            sb.append(o) ;
        }
        return sb.toString() ;
    }

    public Object doFake(String keyword){

        String args = RegUtils.reg_args(keyword) ;
        String[] arguments = null ;
        if(StringUtils.isNotBlank(args)){
            arguments = args.split(",");
        }

        String  name  = RegUtils.reg_class_name(keyword);
        String  methodName  = RegUtils.reg_method(keyword);
        String className = "com.github.javafaker."+name ;
        if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(methodName)){
            try {
                Class<?> clazz = Class.forName(name);
                Object o = clazz.newInstance();
                Method method = clazz.getMethod(methodName);
                Object retVal = method.invoke(o, args);
                return retVal ;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(methodName.equals("enum")){
            return FakeUtils.fake_enum(arguments) ;
        }

        return "" ;

    }

    public static void main(String[] args) {
        System.out.println(RegUtils.reg_method("inc(id,1191452361)]'"));
    }


}
